import blue
from entities import entityConstants
locals().update(entityConstants)
exports = {}
STRUCTURE_UNANCHORED = 0
STRUCTURE_ANCHORED = 1
STRUCTURE_ONLINING = 2
STRUCTURE_REINFORCED = 3
STRUCTURE_ONLINE = 4
STRUCTURE_OPERATING = 5
STRUCTURE_VULNERABLE = 6
STRUCTURE_SHIELD_REINFORCE = 7
STRUCTURE_ARMOR_REINFORCE = 8
STRUCTURE_INVULNERABLE = 9
exports['pos.STRUCTURE_ANCHORED'] = STRUCTURE_ANCHORED
exports['pos.STRUCTURE_UNANCHORED'] = STRUCTURE_UNANCHORED
exports['pos.STRUCTURE_ONLINING'] = STRUCTURE_ONLINING
exports['pos.STRUCTURE_REINFORCED'] = STRUCTURE_REINFORCED
exports['pos.STRUCTURE_ONLINE'] = STRUCTURE_ONLINE
exports['pos.STRUCTURE_OPERATING'] = STRUCTURE_OPERATING
exports['pos.STRUCTURE_VULNERABLE'] = STRUCTURE_VULNERABLE
exports['pos.STRUCTURE_SHIELD_REINFORCE'] = STRUCTURE_SHIELD_REINFORCE
exports['pos.STRUCTURE_ARMOR_REINFORCE'] = STRUCTURE_ARMOR_REINFORCE
exports['pos.STRUCTURE_INVULNERABLE'] = STRUCTURE_INVULNERABLE
exports['pos.DISPLAY_NAMES'] = {STRUCTURE_ANCHORED: mls.UI_GENERIC_ANCHORED,
 STRUCTURE_UNANCHORED: mls.UI_GENERIC_UNANCHORED,
 STRUCTURE_ONLINING: mls.UI_GENERIC_ONLINING,
 STRUCTURE_REINFORCED: mls.UI_GENERIC_REINFORCED,
 STRUCTURE_ONLINE: mls.UI_GENERIC_ONLINE,
 STRUCTURE_OPERATING: mls.UI_GENERIC_OPERATING,
 STRUCTURE_VULNERABLE: mls.UI_GENERIC_VULNERABLE,
 STRUCTURE_SHIELD_REINFORCE: mls.UI_GENERIC_SHIELD_REINFORCEMENT,
 STRUCTURE_ARMOR_REINFORCE: mls.UI_GENERIC_ARMOR_REINFORCEMENT,
 STRUCTURE_INVULNERABLE: mls.UI_GENERIC_INVULNERABLE}
ANCHORED_STRUCTURE_STATES = (STRUCTURE_ANCHORED,
 STRUCTURE_ONLINING,
 STRUCTURE_REINFORCED,
 STRUCTURE_ONLINE,
 STRUCTURE_OPERATING,
 STRUCTURE_VULNERABLE,
 STRUCTURE_SHIELD_REINFORCE,
 STRUCTURE_ARMOR_REINFORCE,
 STRUCTURE_INVULNERABLE)
db2Entity = {STRUCTURE_UNANCHORED: [STATE_UNANCHORING, STATE_UNANCHORED],
 STRUCTURE_ANCHORED: [STATE_ANCHORING, STATE_ANCHORED],
 STRUCTURE_ONLINING: [STATE_ONLINING, STATE_ONLINING],
 STRUCTURE_REINFORCED: [STATE_REINFORCED],
 STRUCTURE_ONLINE: [STATE_IDLE],
 STRUCTURE_OPERATING: [STATE_OPERATING, STATE_IDLE],
 STRUCTURE_VULNERABLE: [STATE_VULNERABLE]}

def GetEntityStatesForDBState(dbState):
    return db2Entity.get(dbState, [])


exports['pos.GetEntityStatesForDBState'] = GetEntityStatesForDBState

def IsStructureAnchored(item, settings):
    if (settings.state in ANCHORED_STRUCTURE_STATES):
        return 1
    else:
        if ((settings.state == STRUCTURE_UNANCHORED) and (settings.stateTimestamp is not None)):
            diffMs = ((blue.os.GetTime() - settings.stateTimestamp) / 10000)
            return (diffMs < sm.services['dogmaIM'].GetTypeAttribute2(item.typeID, const.attributeUnanchoringDelay))
        return 0


exports['pos.IsStructureAnchored'] = IsStructureAnchored
ONLINE_STABLE_STATES = (STRUCTURE_REINFORCED,
 STRUCTURE_ONLINE,
 STRUCTURE_OPERATING,
 STRUCTURE_VULNERABLE,
 STRUCTURE_INVULNERABLE,
 STRUCTURE_SHIELD_REINFORCE,
 STRUCTURE_ARMOR_REINFORCE)
exports['pos.ONLINE_STABLE_STATES'] = ONLINE_STABLE_STATES

def Entity2DB(activityState):
    if (activityState in (STATE_UNANCHORING, STATE_UNANCHORED)):
        return STRUCTURE_UNANCHORED
    if (activityState in (STATE_ANCHORING, STATE_ANCHORED)):
        return STRUCTURE_ANCHORED
    if (activityState == STATE_ONLINING):
        return STRUCTURE_ONLINING
    if (activityState == STATE_REINFORCED):
        return STRUCTURE_REINFORCED
    if (activityState == STATE_VULNERABLE):
        return STRUCTURE_VULNERABLE
    if (activityState == STATE_INVULNERABLE):
        return STRUCTURE_INVULNERABLE
    if (activityState == STATE_OPERATING):
        return STRUCTURE_OPERATING
    if (activityState == STATE_SHIELD_REINFORCE):
        return STRUCTURE_SHIELD_REINFORCE
    if (activityState == STATE_ARMOR_REINFORCE):
        return STRUCTURE_ARMOR_REINFORCE
    if (activityState >= STATE_IDLE):
        return STRUCTURE_ONLINE


exports['pos.Entity2DB'] = Entity2DB

